<?php
class PloyPinGeneratesController extends AppController 
{
	public $name = 'PloyPinGenerates';
	public $uses=array('PloyPinGenerate','GoodsBrand');
	public $per_page = 20;
	public $start_arr = array('online'=>'已上线','offline'=>'未上线','cancel'=>'取消');
	public $local_codes = array();

	public function index(){
		App::import('Vendor', 'page');
		// $conditions['del'] = 1;
		$totalcount = $this->PloyPinGenerate->find('count');
		$page= new Page($totalcount,$this->per_page);
		$list = $this->PloyPinGenerate->find('all',array('order'=>array('pin_id desc'), 'limit'=>$page->firstRow.','.$page->listRows));
		$brandArr=$this->brandList();
		foreach ($list as $key => $value) {
			$brand_id=$value['PloyPinGenerate']['brand_id'];
			$list[$key]['PloyPinGenerate']['brand_name']=$brandArr[$brand_id];
		}
		$this->set('page',$page->show());
		$this->set('list', $list);
		$this->set('start_arr', $this->start_arr);
	}
	public function add(){

		if(!empty($this->data)){
			//添加pinCode
			$this->data['PloyPinGenerate']['pin_key']=$this->PloyPinGenerate->generate_secret();
			if($this->PloyPinGenerate->save($this->data)){
				
				$message = '添加PINCODE成功！';
				$this->flash($message, 'index');
			}else{
				$message = '添加PINCODE出错！';
				$this->flash($message, 'index',3,'error');
			}
			
			exit();
		}
		$this->set('brands',$this->brandList());
		$this->set('batchs',$this->PloyPinGenerate->batchList());
	}
/*
*下载显示
*by River
*/	
	public function download_nums() {
		$arr=explode('_', substr($_GET['url'], 33));
		$this->set('pin_id',$arr[0]);
		$this->set('pin_name',$arr[1]);
	}
/*
*下载操作
*by River
*/
	// public function downLoad()

	// {

	// 	$sql='select brand_id,batch,pin_gen_quantity,pin_key from `ploy_pin` where pin_id= '.$this->data['PloyPinGenerate']["pin_id"];
	// 	$dataArr=$this->PloyPinGenerate->query($sql);
	// 	$prefix=$dataArr[0]['ploy_pin']['brand_id'].$dataArr[0]['ploy_pin']['batch'];
	// 	for ($i=1; $i <= $this->data['PloyPinGenerate']["download_nums"]; $i++) { 
	// 		$quantityId=$dataArr[0]['ploy_pin']['pin_gen_quantity']+$i;
	// 		if ($pinCode=$this->PloyPinGenerate->_producePinCode($prefix,$quantityId,$dataArr[0]['ploy_pin']['pin_key'])) {
	// 			$aPinCodes[]=$pinCode;
	// 		}
	// 	}
	// 	$fileName=$prefix;
	// 	$this->PloyPinGenerate->outPutTxt($aPinCodes,$fileName);//给客户导出txt
	// 	$this->PloyPinGenerate->updateQuantity($this->data['PloyPinGenerate']["pin_id"],$quantityId);
	// 	exit;
	// }
/*
*下载操作2
*by River
*/
	public function downLoad() 
	{
		$sql='select brand_id,batch,pin_gen_quantity,pin_key from `ploy_pin` where pin_id= '.$this->data['PloyPinGenerate']["pin_id"];
		$dataArr=$this->PloyPinGenerate->query($sql);
		set_time_limit(0);
		$prefix=$dataArr[0]['ploy_pin']['brand_id'].$dataArr[0]['ploy_pin']['batch'];
		$this->PloyPinGenerate->startGenerate();
		for ($i=1; $i <= $this->data['PloyPinGenerate']["download_nums"]; $i++) { 
			$quantityId=$dataArr[0]['ploy_pin']['pin_gen_quantity']+$i;//该类PINCODE的顺序号
			$pinCode=$this->PloyPinGenerate->_producePinCode($prefix,$quantityId,$dataArr[0]['ploy_pin']['pin_key']);
			if($i < $this->data['PloyPinGenerate']["download_nums"]) {
				$this->writeTxt($pinCode);
			} else {
				$this->writeTxt($pinCode, true);
			}
			
		}
		$fileName=$prefix.'_'.$this->data['PloyPinGenerate']["download_nums"].'_'.date("md");
		$this->PloyPinGenerate->downloadFile($fileName);//给客户导出txt  
		$this->PloyPinGenerate->updateQuantity($this->data['PloyPinGenerate']["pin_id"],$quantityId);
		exit;
	}
	
	/*
	*品牌列表
	*/
	public function brandList()
	{
		$brandList=array(
			'null'=>'请选择品牌',
		);
		$brandArr=$this->GoodsBrand->find('all', array('conditions'=>array('pin_brand_id !='=>''),'fields'=>array('pin_brand_id','name'),'order' => 'pin_brand_id ASC'));
		foreach ($brandArr as $key => $value) {
			$brandList[$value['GoodsBrand']['pin_brand_id']]=$value['GoodsBrand']['name'];
		}
		return $brandList;
	}

	protected function writeTxt($code, $force_write = false) {
		$this->local_codes[] = $code;
		if(count($this->local_codes)<20000 && !$force_write) {
			return;
		}
		$this->PloyPinGenerate->outPutText($this->local_codes);
		$this->local_codes = array();
	} 
}
?>